package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

/**
 * @author xupkun
 * @date 2024/11/6
 */
@Slf4j
@LiteflowComponent(value = "AgvPick", name = "[轮对/轮轴]取缓存架完成")
@AllArgsConstructor
public class AgvPick extends NodeComponent {

    private final RobotDriverService robotDriverService;

    private final RobotMapPosService robotMapPosService;

    private final RobotService robotService;
    private final RestTemplate restTemplate;


    @Override
    public void process() throws Exception {
        FlowContextDto dto =  this.getRequestData();
        Robot robot = robotService.getDefaultRobot();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String sourceName = ticketsDto.getModalData().get("sourcePosName")== null ?
                String.valueOf(ticketsDto.getModalData().get("sourcePosCode")):
                String.valueOf(ticketsDto.getModalData().get("sourcePosName"));


        String target = ticketsDto.getModalData().get("targetPosName")== null ?
                String.valueOf(ticketsDto.getModalData().get("targetPosCode")):
                String.valueOf(ticketsDto.getModalData().get("targetPosName"));
        RobotMapPos robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                .eq(RobotMapPos::getName, sourceName)
                .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        log.info("AGV取完成 {}", robotMapPos.getName());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sbID", robot.getRobotSn());
        jsonObject.put("des", target);
        jsonObject.put("src", robotMapPos.getName());
        jsonObject.put("srcnum", Integer.parseInt((String) ticketsDto.getModalData().get("srcnum")));
        jsonObject.put("data", (String) ticketsDto.getModalData().get("pjData"));
        HttpEntity<String> strEntity = new HttpEntity<String>(jsonObject.toString(), headers);
        log.info("缓存完成接口:" + strEntity.toString());
        String result = restTemplate.postForObject("http://172.24.3.1:8090/api/v1/centreApp/agv/pick/",
                strEntity, String.class);
        log.info("reset session" + result);
    }

}
